#include "../LogicProtocol_CommHeader.h"
#include "LogicProtocol_QueryPacket.h"
/********************************************************************
//	Created:	2018/8/24   12:10
//	Filename: 	/NetEngine_Linux/NetEngine_SourceCode/SLEngine_Logic/SLEngine_Protocol/SLProtocol_Packet/SLProtocol_Packet.cpp
//	File Path:	/NetEngine_Linux/NetEngine_SourceCode/SLEngine_Logic/SLEngine_Protocol/SLProtocol_Packet/
//	File Base:	SLProtocol_Packet
//	File Ext:	cpp
//  Project:    NetEngien_Linux
//	Author:		Dowflyon
//	Purpose:    协议打包解包工具
//	History:
*********************************************************************/
CLogicProtocol_QueryPacket::CLogicProtocol_QueryPacket()
{
}
CLogicProtocol_QueryPacket::~CLogicProtocol_QueryPacket()
{
}
//////////////////////////////////////////////////////////////////////////
//                       公有函数
//////////////////////////////////////////////////////////////////////////
BOOL CLogicProtocol_QueryPacket::LogicProtocol_QueryPacket_FileReq(LPCTSTR lpszMsgJson,TCHAR *ptszSQLName)
{
    LogicProtocol_IsErrorOccur = FALSE;

    if ((NULL == lpszMsgJson) || (NULL == ptszSQLName))
    {
        LogicProtocol_IsErrorOccur = TRUE;
        LogicProtocol_dwErrorCode = ERROR_WINAPT_LOGIC_PROTOCOL_QUERY_FILEREQ_PARAMENT;
        return FALSE;
    }
    Json::Value st_JsonRoot;
    Json::Reader st_JsonReader;

    if (!st_JsonReader.parse(lpszMsgJson,st_JsonRoot))
    {
        LogicProtocol_IsErrorOccur = TRUE;
        LogicProtocol_dwErrorCode = ERROR_WINAPT_LOGIC_PROTOCOL_QUERY_FILEREQ_JSON;
        return FALSE;
    }
    _tcscpy(ptszSQLName,st_JsonRoot["tszSQLName"].asCString());

    return TRUE;
}
BOOL CLogicProtocol_QueryPacket::LogicProtocol_QueryPacket_FileRep(NETENGINE_PROTOCOLHDR *pSt_ProtocolHdr,list<LOGICSQL_SOFTLIST> *pSt_SoftList,TCHAR *ptszMsgBuffer,int *pInt_Len)
{
    LogicProtocol_IsErrorOccur = FALSE;

    if ((NULL == pSt_ProtocolHdr) || (NULL == pSt_SoftList) || (NULL == ptszMsgBuffer) || (NULL == pInt_Len))
    {
        LogicProtocol_IsErrorOccur = TRUE;
        LogicProtocol_dwErrorCode = ERROR_WINAPT_LOGIC_PROTOCOL_QUERY_FILEREP_PARAMENT;
        return FALSE;
    }
    Json::Value st_JsonRoot;
    Json::Value st_JsonArray;

    st_JsonRoot["Code"] = 0;
    st_JsonRoot["Msg"] = "查询成功";

    list<LOGICSQL_SOFTLIST>::const_iterator stl_ListIterator = pSt_SoftList->begin();
    for (;stl_ListIterator != pSt_SoftList->end();stl_ListIterator++)
    {
        Json::Value st_JsonObject;
        st_JsonObject["tszName"] = stl_ListIterator->tszName;
        st_JsonObject["tszFullName"] = stl_ListIterator->tszFullName;
        st_JsonObject["tszVersion"] = stl_ListIterator->tszVersion;
        st_JsonObject["tszAuthor"] = stl_ListIterator->tszAuthor;
        st_JsonObject["tszProvider"] = stl_ListIterator->tszProvider;
        st_JsonObject["tszBuildTime"] = stl_ListIterator->tszBuildTime;
        st_JsonObject["tszContact"] = stl_ListIterator->tszContact;
        st_JsonObject["tszProjectUrl"] = stl_ListIterator->tszProjectUrl;
        st_JsonObject["nArchType"] = stl_ListIterator->nArchType;
        st_JsonObject["dlDlSize"] = stl_ListIterator->dlDlSize;
        st_JsonObject["dlInstallSize"] = stl_ListIterator->dlInstallSize;
        st_JsonObject["tszDlUrl"] = stl_ListIterator->tszDlUrl;
        st_JsonObject["tszFileMD5"] = stl_ListIterator->tszFileMD5;
        st_JsonObject["tszDepModule"] = stl_ListIterator->tszDepModule;
        st_JsonObject["tszDesInfo"] = stl_ListIterator->tszDesInfo;
        st_JsonArray.append(st_JsonObject);
    }
    st_JsonRoot["Count"] = st_JsonArray.size();
    st_JsonRoot["List"] = st_JsonArray;

    pSt_ProtocolHdr->unPacketSize = st_JsonRoot.toStyledString().length();
    pSt_ProtocolHdr->unOperatorCode = NETENGINE_WINAPT_PROTOCOL_CODE_SEARCH_RESPONSE;

    *pInt_Len = sizeof(NETENGINE_PROTOCOLHDR) + pSt_ProtocolHdr->unPacketSize;
    memcpy(ptszMsgBuffer,pSt_ProtocolHdr,sizeof(NETENGINE_PROTOCOLHDR));
    memcpy(ptszMsgBuffer + sizeof(NETENGINE_PROTOCOLHDR),st_JsonRoot.toStyledString().c_str(),pSt_ProtocolHdr->unPacketSize);

    return TRUE;
}
